<template>
  <div>
    <el-drawer
    v-model="dialogVisible"
    title='新增'
    @close="reset"
  >
    <div class="scene-dialog">
      <el-form 
        ref="articleFormRef"
        :model="tableItem" 
        :rules="rules"
        label-width="100"
        label-position="top"
      >
        <el-form-item label="变量名称" prop="varName">
          <el-input v-model="tableItem.varName" />
        </el-form-item>

        <el-form-item label="变量单位" prop="varUnit">
          <el-input v-model="tableItem.varUnit" />
        </el-form-item>

        <el-form-item label="数据格式" prop="dataFormat">
          <el-select v-model="tableItem.dataFormat" placeholder="请选择">
            <el-option label="位" :value="1" />
            <el-option label="9位 无符号" :value="2" />
            <el-option label="8位 BCD" :value="3" />
            <el-option label="16位 有符号" :value="4" />
            <el-option label="32位浮点 (DCBA)" :value="5" />
          </el-select>
        </el-form-item>

        <el-form-item label="数字格式" prop="numberFormat">
          <el-select v-model="tableItem.numberFormat" placeholder="请选择">
            <el-option label="整数" :value="1" />
            <el-option label="保留1位小数" :value="2" />
            <el-option label="保留2位小数" :value="3" />
            <el-option label="保留3位小数" :value="4" />
            <el-option label="保留4位小数" :value="5" />
          </el-select>
        </el-form-item>

        <el-form-item label="读写方式" prop="readWriteMode">
          <el-select v-model="tableItem.readWriteMode" placeholder="请选择">
            <el-option label="读写" :value="1" />
            <el-option label="只读" :value="2" />
            <el-option label="只写" :value="3" />
          </el-select>
        </el-form-item>
      </el-form>
    </div>
      
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button
          type="primary"
          @click="createData"
        >提交</el-button>
      </span>
    </template>
    </el-drawer>
  </div>


</template>
<script lang="ts" setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { addVar } from '@/api/collect'
import { validate } from '@/utils/validate'

const tableItem = reactive({
  templateId: null,
  varName: '',
  varUnit: '',
  dataFormat: '',
  numberFormat: '',
  readWriteMode: null,
})

const rules = reactive({
  varName: [{ required: true, message: '请输入变量名称', trigger: 'blur' }],
  dataFormat: [{ required: true, message: '请选择数据格式', trigger: 'change' }],
  numberFormat: [{ required: true, message: '请选择数字格式', trigger: 'change' }],
  readWriteMode: [{ required: true, message: '请选择读写方式', trigger: 'change' }],
})

const dialogVisible = ref(false)
const dialogStatus = ref('create')

const reset = () => {
  if (!articleFormRef) return
  articleFormRef.value.resetFields()
}

const articleFormRef = ref(null)

const emit = defineEmits(['listChange'])

const createData = async () => {
  const valid = await validate(articleFormRef)
  if (valid) {
    addVar(tableItem)
    .then(() => {
      // TODO use res data
      emit('listChange')
      dialogVisible.value = false
      ElMessage.success('创建成功！')
    })
    .catch(() => {
      dialogVisible.value = false
      ElMessage.error('创建失败！')
    })
  }
}

defineExpose({ dialogVisible, tableItem, dialogStatus, reset })

</script>
<style lang="scss" scoped>
:deep(.el-drawer__header) {
  margin-bottom: 20px;
}
:deep(.el-drawer__body) {
  padding-top: 0px;
}
:deep(.el-drawer__footer) {
  text-align: center;
}
.scene-dialog {

}
</style>
